package uk.ac.aber.paws.server.net;

import static uk.ac.aber.paws.server.config.Constants.*;
import static uk.ac.aber.paws.server.config.NetConstants.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Hashtable;
import java.util.logging.Level;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import uk.ac.aber.paws.server.config.ConfigManager;
import uk.ac.aber.paws.server.core.PAWSServer;


/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * SendMessage.java
 * 
 * This class compiles and sends message via the ServerSocket to the client computers.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class SendMessage {
	
	/**
	 * This procedures opens up a connection to the client and requests the current idle time of
	 * the users logged in. 
	 * @param fqdn The computer name to be queried
	 * @param port The port number on which connect the socket to on the client
	 * @return A hashtable containing user id referenced with their idle time
	 */
	public static Hashtable<String,Long> requestIdleTimeMessage(String fqdn, int port) {
		
		Hashtable<String,Long> returnVal = null;
		
		try {
			
			SSLSocketFactory SSLSocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			SSLSocket socket = (SSLSocket)SSLSocketfactory.createSocket(fqdn,port);
			
			final String[] enabledCipherSuites = { "SSL_DH_anon_WITH_RC4_128_MD5" };
		    socket.setEnabledCipherSuites(enabledCipherSuites);
		   
			socket.setSoTimeout(10000);
			
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());             
			oos.writeObject(REQUEST_CLIENT_IDLE_TIME); 
		
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

			Hashtable<String,Long> response = (Hashtable<String,Long>)ois.readObject();
			
			PAWSServer.LOG_FILE.log(Level.ALL,"Idle Time Response: "+response.toString());
			
			returnVal = response;
			
			socket.close();
			
		}
		catch (java.net.SocketTimeoutException ex) {
			return returnVal;
		}
		catch (Exception ex)  {
			return returnVal;
		}
			
		return returnVal;
	}

	/**
	 * This procedures opens up a connection to the client and requests the current idle time of
	 * the users logged in. 
	 * @param fqdn The computer name to be queried
	 * @param port The port number on which connect the socket to on the client
	 * @return A hashtable containing user id referenced with their idle time
	 */
	public static Integer requestCallDBLength(int port) {
		
		Integer returnVal = -1;
		
		try {
			
			SSLSocketFactory SSLSocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
			SSLSocket socket = (SSLSocket)SSLSocketfactory.createSocket("127.0.0.1",port);
			
			final String[] enabledCipherSuites = { "SSL_DH_anon_WITH_RC4_128_MD5" };
		    socket.setEnabledCipherSuites(enabledCipherSuites);
		   
			socket.setSoTimeout(2000);
			
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());             
			oos.writeObject(MANAGEMENT_GET_DBCALL_INFO); 
		
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

			Integer response = (Integer)ois.readObject();
					
			returnVal = response;
			
			socket.close();
			
		}
		catch (java.net.SocketTimeoutException ex) {
			ex.printStackTrace();
			return returnVal;
		}
		catch (Exception ex)  {
			ex.printStackTrace();
			return returnVal;
		}
			
		return returnVal;
	}
}
